package request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

/**
 * 获取请求参数通用方式：无论是get还是post请求方式都可以使用下列方法来获取请求参数
 *
 * 1. String getParameter(String name):根据参数名称获取值   username=zs&password=123
 * 2. String[] getParameterValues(String name):根据参数名称获取参数值的数组  hobby=xx&hobby=game
 * 3. Enumeration<String> getParameterNames():获取所有请求参数的名称
 * 4. Map<String,String[]> getParameterMap():获取所有参数的map集合
 *
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-03-20-21:18 21:18
 */
@WebServlet("/myRequest6")
public class MyRequest6 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //根据参数名称获取值,获取username的值
        String username = request.getParameter("username");
        System.out.println(username);
        //String[] getParameterValues(String name):根据参数名称获取参数值的数组  hobby=xx&hobby=game
        String[] hobbies = request.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }
        //Enumeration<String> getParameterNames():获取所有请求参数的名称
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String s = parameterNames.nextElement();
            System.out.println(s);
            System.out.println("-------------");
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        //Map<String,String[]> getParameterMap():获取所有参数的map集合
        Set<String> strings = parameterMap.keySet();
        for (String key : strings) {
            String[] values = parameterMap.get(key);
            for (String s : values) {
                System.out.println(key+"="+s);
            }
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
